/*绘制线图和柱图*/

//颜色表
var colors = ["#0000FF","#8A2BE2","#A52A2A","#DEB887","#5F9EA0","#7FFF00","#D2691E","#FF7F50",
"#6495ED","#00F8DC","#DC143C","#00008B","#008B8B","#B8860B","#A9A9A9","#006400","#BDB76B",
"#8B008B","#556B2F","#FF8C00","#9932CC","#8B0000","#E9967A","#8FBC8B","#483D8B","#2F4F4F",
"#00CED1","#9400D3","#FF1493","#00BFFF","#696969","#1E90FF","#B22222","#228B22","#FF00FF",
"#FFD700","#DAA520","#808080","#008000","#ADFF2F","#FF69B4","#CD5C5C","#4B0082","#F0E68C",
"#7CFC00","#FFFACD","#ADD8E6","#F08080","#E0FFFF","#FAFAD2","#90EE90","#D3D3D3","#FFB6C1",
"#FFA07A","#20B2AA","#87CEFA","#778899","#B0C4DE","#FFFFE0","#00FF00","#32CD32","#FAF0E6",
"#FF00FF","#800000","#66CDAA","#0000CD","#BA55D3","#9370DB","#3CB371","#7B68EE","#00FA9A",
"#48D1CC","#C71585","#191970","#F5FFFA","#FFE4E1","#FFE4B5","#FFDEAD","#000080","#FDF5E6",
"#808000","#6B8E23","#FFA500","#FF4500","#DA70D6","#EEE8AA","#98FB98","#AFEEEE","#DB7093",
"#FFEFD5","#FFDAB9","#CD853F","#FFC0CB","#DDA0DD","#B0E0E6","#800080","#FF0000","#BC8F8F",
"#4169E1","#8B4513","#FA8072","#F4A460","#2E8B57","#FFF5EE","#A0522D","#C0C0C0","#87CEEB",
"#6A5ACD","#708090","#FFFAFA","#00FF7F","#4682B4","#D2B48C","#008080","#D8BFD8","#FF6347",
"#40E0D0","#EE82EE","#F5DEB3","#FFFFFF","#F5F5F5","#FFFF00","#9ACD32"];

//背景色��ͼ
var BackColor = '#FFFFFF';


/*显示图形*/
function enVMLChart()
{
    //alert(document.getElementById('VMLChart').style.display);
	document.getElementById('disVMLChart').style.display = '';
    document.getElementById('enVMLChart').style.display = 'none';
    document.getElementById('VMLChart').style.display = '';
    
    //如果是第一次显示图形则先从cube中获取数据
    getValue();
    
	//绘制图形
    if(null == GchartType || GchartType < 1 || GchartType > 3)
    {
        drawChart(DEFAULTCT);
    }
    else
    {
        drawChart(GchartType);
    }
}

/*隐藏图形*/
function disenVMLChart()
{
    document.getElementById('disVMLChart').style.display = 'none';
    document.getElementById('enVMLChart').style.display = '';
    document.getElementById('VMLChart').style.display = 'none';
}

//折线图
function drawpoly(xsa,yva,ct)
{    
    var PicWidth = 550;
    var PicHeight = 250;  
    //放大倍数	
	var zoomRate=1;
    //折线图二维区域大小
	var coordx=1000;
	var coordy=1000;  
	//ȡ�����获取y轴最大值
	var maxValue="" + 0;
	for (var i=0;i<yva.length;i++){
		for (var j=0;j<yva[0].length;j++)
        {
			if (parseInt(yva[i][j]) > maxValue)
            {
				maxValue=parseInt(yva[i][j]);
            }
        }       
    }           	
	var yMaxScore = maxValue * 1.05;
	var rate = parseInt(Math.exp( parseInt(Math.log(yMaxScore)/Math.LN10)*Math.LN10))/10
	var t=parseInt(String(yMaxScore).substring(0,1));
	if (t >5)		rate *=10;
	else if (t>2)		rate *=5;
	else if (t>1)		rate *=4;
	else		rate *= 2
	
    //x轴最大值
	var xMaxScore = xsa.length + 1;
	
	//��ʼ��ͼ
	var result="";
	result += "<center><v:group style='width:" + PicWidth + "px;height:"+ PicHeight +"px;position:relative;' CoordSize='"+coordx*zoomRate+","+coordy*zoomRate+"'>";
		
	//����
	//改px result += "<v:rect fillcolor='"+ BackColor +"' style='Z-INDEX:0;width:"+coordx+";height:"+coordy+";position:relative' strokecolor='" + BackColor + "' ></v:rect>";
	 result += "<v:rect fillcolor='"+ BackColor +"' style='Z-INDEX:0;width:"+coordx+"px;height:"+coordy+"px;position:relative' strokecolor='" + BackColor + "' ></v:rect>";
	
    //绘制Y方向 标注
    var tmStrUnit = "                           " +  chartValueUnit;
    //alert(tmStrUnit);     
   //改px result += "<v:rect fillcolor='#EE0000' style='left:-140px;top:" + (coordy / 2 - 100) +"px;width:10;height:450;position:relative' strokecolor='#000000' >";
   result += "<v:rect fillcolor='#EE0000' style='left:-140px;top:" + (coordy / 2 - 100) +"px;width:10px;height:450px;position:relative' strokecolor='#000000' >";
   
    result += "<v:path  textpathok='True'></v:path> ";   
    result += "<v:textpath style='font-size:10pt;' on='True' string='"+ tmStrUnit + "' ></v:textpath>";    
    result += "</v:rect>";    
    
    //x轴
	result += "<v:line  from='0,"+coordy+"' to='"+coordx+","+coordy+"'  strokeweight='1pt' style='Z-INDEX:1;position:relative' ><v:stroke EndArrow='classic'/></v:line>";
	
    //y轴
	result += "<v:line  from='0,"+coordy+ " to='0,0' strokeweight='1pt' style='Z-INDEX:1;position:relative' ><v:stroke EndArrow='classic'/></v:line>";
	
	//�̶�
	var n=(yva.length*40+40)*coordy;
	var pw = 500*coordy;
	if(pw<n)
		pw=n;
     
    //绘制Y轴标线和标注
    //先根据刻度最大值计算标注应该左让多宽   
    yLength = 70;
    
    if( yMaxScore > 100000)
    {
        var tm = " " + parseInt(yMaxScore);
        yLength = tm.length * 14;
    }
	for (var i=yMaxScore;i>0;i-=rate)
	{						
		result += "<v:line from='"+ 0 +","+(i/yMaxScore*coordy)+"' to='"+coordy+","+(i/yMaxScore*coordy)+"' strokecolor=#c0c0c0><v:stroke dashstyle=dash /></v:line>"
		//改px result += "<v:rect fillcolor='" + BackColor + "' style='Z-INDEX:0;top:"+(i/yMaxScore*coordy-25)+";left:-" + yLength + ";width:" + yLength + ";height:90;position:relative' strokecolor='" + BackColor + "'>"
		result += "<v:rect fillcolor='" + BackColor + "' style='Z-INDEX:0;top:"+(i/yMaxScore*coordy-25)+"px;left:-" + yLength + "px;width:" + yLength + "px;height:90px;position:relative' strokecolor='" + BackColor + "'>"
		
		result += "<v:TextBox inset='0,0,0,0' style='font-size:"+8/zoomRate+"pt;'>"+parseInt((yMaxScore-i)*100)/100+"</v:TextBox></v:rect>"					
	}
	
    //绘制X轴标注
	for (var i=0;i<xsa.length;i++)
	{
		//改px result += "<v:rect fillcolor='" + BackColor + "' style='Z-INDEX:0;top:"+(coordy+5)+";left:" +(i/xsa.length*(coordx-1))+";width:"+(coordx/xsa.length)+";height:90;position:relative' strokecolor='" + BackColor + "'>"
		result += "<v:rect fillcolor='" + BackColor + "' style='Z-INDEX:0;top:"+(coordy+5)+"px;left:" +(i/xsa.length*(coordx-1))+"px;width:"+(coordx/xsa.length)+";height:90px;position:relative' strokecolor='" + BackColor + "'>"
		
		result += "<v:TextBox inset='-10,0,0,0' style='font-size:"+8/zoomRate+"pt;TEXT-ALIGN:center'>"+xsa[i]+"</v:TextBox></v:rect>"
	}
	
    //折线宽度
    var lineWid = 2;
    for (j=0;j<yva.length;j++)
    {
       st = ""
       sp = "";
	   //绘制折线中标记点
	   for (i=0;i<yva[j].length;i++)
	   {
	       st += parseInt(i/(yva[j].length)*coordx + coordx/(yva[j].length)/2)+","+ parseInt(coordy-yva[j][i]/yMaxScore*coordy)+" ";
           
	      //改px sp +="<v:oval strokeweight='1px' strokecolor='#000000' fillcolor='#f75017' title='名称：" + ct[j] +"\n" + xStrName + ":" + xsa[i] + "\n数值：" + yva[j][i] + "' style='Z-INDEX:0;top:" + parseInt(coordy-yva[j][i]/yMaxScore*coordy-4) + ";left:" + parseInt(i/(yva[j].length)*coordx + coordx/(yva[j].length)/2-4) + ";width:10;height:15;position:relative'></v:oval>" 
	  			sp +="<v:oval strokeweight='1px' strokecolor='#000000' fillcolor='#f75017' title='名称：" + ct[j] +"\n" + xStrName + ":" + xsa[i] + "\n数值：" + yva[j][i] + "' style='Z-INDEX:0;top:" + parseInt(coordy-yva[j][i]/yMaxScore*coordy-4) + "px;left:" + parseInt(i/(yva[j].length)*coordx + coordx/(yva[j].length)/2-4) + "px;width:10px;height:15px;position:relative'></v:oval>" 
	  
	   }
        
       //数据折线
	   result +="<v:PolyLine strokeweight='" + lineWid + "px' filled='false' title='"+ct[j]+"' Points= ' "+st+" ' style='position:relative;' strokecolor='" + colors[j] + "'/>"
	   result +=sp
	}
        
    //绘制图例
    result += drawLengend(coordx,xsa,yva,ct,lineWid,zoomRate);
	//����ͼ		
	result +="</v:group></center>";
	
	return result;
}


//柱图
function drawpole(xsa,yva,ct)
{	
    //图形宽高
    var PicWidth = 550;
    var PicHeight = 250;
    
	//缩放倍数 
    var zoomRate= 1; //放大 1 / zoomRate倍
    
    //柱图绘制二维区域
    var coordx=1000;
    var coordy=1000;    

	this.DrGroup=new Array();
	
	if(yva.length<1)return;

	var intMax=0;
	var intTotal=yva[0].length + 1; //一个集包含几个柱
	var intPWidth=parseInt(1800 / 4 * coordx / 1000 / intTotal,10);
	var intPad=intPWidth * 2;
	intPWidth=(intPWidth>300?300:intPWidth);
    
    var interVal1 = 10; //柱与柱之间的间隔
    var interVal2 = 15; //柱集之间间隔
	
    //获取y方向最大值
	for(var j=0;j<yva.length;j++)
    {
		for(var i=0;i<yva[j].length;i++)
        {
			intMax=(intMax < parseInt(yva[j][i])?parseInt(yva[j][i]):intMax)
		}
	}
    
    //得到Y轴标注最大标注值
    intMax = parseInt(intMax);
	intMax=(parseInt(intMax / Math.pow(10,(intMax+"").length - 1)) + 1) * Math.pow(10,(intMax+"").length - 1)
	var sint = 35/yva.length + (intPad/yva.length);
	
	var w=1;
	var strObj='DR_Group'
	while(w > 0)
    {
		if(document.all(strObj + w)==null)break;
		w++
	}
	this.DrGroup[this.DrGroup.length++]=(strObj + w);

	var result = "<center><v:group style='width:" + PicWidth + "px;height:"+ PicHeight +"px;position:relative;' CoordSize='"+coordx*zoomRate+","+coordy*zoomRate+"'>";
    
   //改px result += "<v:rect fillcolor='" + BackColor + "' style='Z-INDEX:0;width:"+coordx+";height:"+coordy+";position:relative' strokecolor='" + BackColor + "' ></v:rect>";	
 	result += "<v:rect fillcolor='" + BackColor + "' style='Z-INDEX:0;width:"+coordx+"px;height:"+coordy+"px;position:relative' strokecolor='" + BackColor + "' ></v:rect>";	

 
    var j = yva.length;
    var i = yva[0].length;
    var st = yva.length/10;
    
    //每个柱子宽度
    //var polWid = sint/2 * coordx / 1000 - 100 / 4 * coordx / 1000 / yva.length;
    var polWid = sint * coordx / 1000 - 100 / 4 * coordx / 1000 / yva.length;
    
    var tmWid = 120 + (polWid + interVal1) * (j * i ) + (interVal2 -  interVal1) * j * i; 
    tmWid += 20;
   
     
    //定义图形的7个顶点
    var Point1X = coordx * 100  / 1000;
    var Point1Y = coordy * 900  / 1000;
    
    var Point2X = coordx * 50   / 1000;
    var Point2Y = coordy * 950  / 1000;
    
    var Point3X = coordx * (tmWid - 50)  / 1000;
    var Point3Y = coordy * 950  / 1000;
    
  
    var Point4X = tmWid;
    var Point4Y = coordy * 900  / 1000;
    
    var Point5X = coordx * 50   / 1000;
    var Point5Y = coordy * 50   / 1000;
    
    var Point6X = coordx * 100  / 1000;
    var Point6Y = coordy * 0    / 1000;
    var Point7X = tmWid;
    var Point7Y = coordy * 0    / 1000;      
     
    //绘制Y方向 标注
    var tmStrUnit = "                           " +  chartValueUnit;   
    //改px result += "<v:rect fillcolor='#EE0000' style='left:-50px;top:"+ ((Point4Y - 100) * coordy / 1000) / 2 +"px;width:30;height:450;position:relative' strokecolor='#000000' >";
   	result += "<v:rect fillcolor='#EE0000' style='left:-50px;top:"+ ((Point4Y - 100) * coordy / 1000) / 2 +"px;width:30px;height:450px;position:relative' strokecolor='#000000' >";
   
    result += "<v:path  textpathok='True'></v:path> ";   
    result += "<v:textpath on='True' style='font-size:10pt;' string='"+ tmStrUnit + "' ></v:textpath>";    
    result += "</v:rect>";
    
     
    //根据7个顶点绘制背景三维框
    result += "<v:PolyLine Points='" + Point1X + "," + Point1Y + " " + Point2X + "," + Point2Y + " " + Point3X + "," +Point3Y + " " + Point4X + "," + Point4Y + "' strokecolor='#0099ff'>\n"       //��
    result += "<v:fill color='#00cfef' angle='45' rotate='t' focus='100%' type='gradient' />\n"
    result += "</v:PolyLine>\n"

    result += "<v:PolyLine Points='" + Point1X + "," + Point1Y + " " + Point2X + "," + Point2Y + " " + Point5X + "," +Point5Y + " " + Point6X + "," + Point6Y + "' strokecolor='#0099ff'>\n"       //��    ��
    result += "<v:fill color='#00cfef' angle='210' rotate='t' focus='100%' type='gradient' />\n"
    result += "</v:PolyLine>\n"
    
    result += "<v:PolyLine Points='" + Point6X + "," + Point6Y + " " + Point7X + "," + Point7Y + " " + Point4X + "," +Point4Y + " " + Point1X + "," + Point1Y + "' strokecolor='#0099ff'>\n"       //��    ��   
    result += "<v:fill color='#00cfef' angle='135' rotate='t' focus='100%' type='gradient' />\n"    //��
    result += "</v:PolyLine>\n"


    //Y轴 横向标线   
	for(var i=1;i<10;i++){
		result += "<v:PolyLine filled='false' Points='" + Point2X * coordx / 1000 + ","+( ( Point4Y + Point5Y )* coordy / 1000 - Point4Y / 10 * coordy / 1000 * i)+" " + Point1X * coordx / 1000 + ","+(Point4Y * coordy / 1000 - Point4Y / 10  * coordy / 1000 * i)+ " " + Point4X * coordx / 1000 + "," +( Point4Y * coordy / 1000 - Point4Y / 10 * coordy / 1000 * i)+"' strokecolor='#0099ff'>\n"
		result += "<v:stroke dashstyle='Dash' />\n"
		result += "</v:PolyLine>\n"
	}
    
    //Y轴标注字体高度
    var fontHeight = 20;
    //Y轴标注
	for(var i=1;i<11;i++)
    {
		//改px result +="<v:RoundRect style='left:0;top:"+(Point4Y * coordy / 1000 + fontHeight / 2 - Point4Y / 10 * coordy / 1000 * i)+";' strokeColor='transparent'>"
		result +="<v:RoundRect style='left:0;top:"+(Point4Y * coordy / 1000 + fontHeight / 2 - Point4Y / 10 * coordy / 1000 * i)+"px;' strokeColor='transparent'>"
		
		result +="<v:textbox inset='0pt,0pt,0pt,0pt' style='font-size:9pt;color:#0000ff;'>"+(i*parseInt(intMax*100 / 10))/100+"</v:textbox></v:RoundRect>"
	}
    var lineWid = 2;
    
    //绘制柱条
    
	var st = yva.length/10;
	for (var j=0;j<yva.length;j++)
    {
		for(var i=0;i<yva[j].length;i++)
        {
	       //定义每个柱X方向起始位置
           var ssss = 120 + (polWid + interVal1) * (i) + (polWid + interVal2) * (j * yva[0].length );	                
           //定义了柱x方向结束位置
           var ss = ssss + polWid;
			
	   		result += "<v:fill color='"+colors[j]+"' angle='100' rotate='t' focus='100%' type='gradient' />";
            
            //注意：title属性设置了区域图形鼠标提示功能
	   		result += "<v:PolyLine title= '名称：" + ct[j] +"\n" + xStrName + ":" + xsa[i] + "\n数值：" + yva[j][i] + "' Points='"
	   			+ ssss + ", " + Point4Y * coordy / 1000 + " "  
	   			+ ssss + ", " + (Point4Y * coordy / 1000 -(yva[j][i]/intMax * Point4Y * coordy / 1000))  + " " 
	   			+ (ss ) + ", " + (Point4Y * coordy / 1000 -(yva[j][i]/intMax * Point4Y * coordy / 1000)) + " " 
	   			+ (ss ) + ", " + Point4Y * coordy / 1000 + "'"
	   			+ " strokecolor='#ffffff'>";
	 		result += "<v:fill color='"+colors[j]+"' angle='10' rotate='t' focus='100%' type='gradient' />\n"
	   		result += "<o:extrusion v:ext='view' on='t' backdepth='0' foredepth='"+(parseInt(intPWidth / 10))+"' />\n"
			result += "</v:PolyLine>\n"
		}
	}
    
	//绘制图例
    result += drawLengend(tmWid + 20,xsa,yva,ct,lineWid,zoomRate);
    
	result +="</v:group></center>";
	return result;
}

//绘制图例
function drawLengend(coordx,xsa,yva,ct,lineWid,zoomRate)
{
    var result = "";
    //绘制图例
    
    //图例外围矩形宽度
    var tmLengendWidth = 100;
    //图例外围矩形高度
    var tmLengendHeight = 100;
    
    //单个字体宽度
    var tmLengFontWid = 25;
    
    //单条图例高度
    var tmLengItemHei = 80;
    
    var tmLengendTop = 15;
    var tmLengendLeft = coordx + 40;
    
    //图例二字高度
    var tmLengFontHei = 70;
        
    //图例线长度
    var lenLineLen = 50; 
    
    //获取图例最大宽度
    for(j = 0; j < yva.length; j++)
    {
       if(ct[j].length*tmLengFontWid > tmLengendWidth)
       {
            tmLengendWidth = ct[j].length*tmLengFontWid;
       }
    }
    
    if(yva.length > 0)
    {
        tmLengendHeight = yva.length * (tmLengItemHei + 5) + tmLengFontHei;
    }
    tmLengendWidth += lenLineLen + 40;
    
    //图例的外框
    //改px result +="<v:rect fillcolor='#9cf' stroked='t' strokecolor='#0099ff' style='Z-INDEX:0;top:" + tmLengendTop + ";left:" + tmLengendLeft +";width:" + tmLengendWidth + ";height:" + tmLengendHeight + ";position:relative'>";
   result +="<v:rect fillcolor='#9cf' stroked='t' strokecolor='#0099ff' style='Z-INDEX:0;top:" + tmLengendTop + "px;left:" + tmLengendLeft +"px;width:" + tmLengendWidth + "px;height:" + tmLengendHeight + "px;position:relative'>";
   
    result += "<v:fill rotate='t' angle='-175' focus='100%' type='gradient'/>";
    result += "<v:shadow on='t' type='single' color='silver' offset='3pt,3pt'/>";
    result += "</v:rect>";
    
    //图例二字
    var tmLeft = tmLengendLeft + tmLengendWidth/2 - 70;
    var tmTop  = tmLengendTop + 10;
    var tmLengFontHei = 70;
    //改px result += "<v:Rect style='position:relative;left:" + tmLeft + ";top:" + tmTop + ";width:100;height:"+ tmLengFontHei +"' stroked='f' filled='f'>";
    result += "<v:Rect style='position:relative;left:" + tmLeft + "px;top:" + tmTop + "px;width:100px;height:"+ tmLengFontHei +"px' stroked='f' filled='f'>";
    
    result += "<v:TextBox inset='8pt,2pt,3pt,3pt' style='font-size:8pt;'><div align='left'><B>图例</B></div></v:TextBox>";
    result += "</v:Rect> " ;
          
    //图例中各个条目
    for (j=0;j<yva.length;j++)
    {            
       result += "<v:line from='"+(tmLengendLeft + 10)+","+(tmLengendTop + tmLengFontHei + (j * tmLengItemHei) + tmLengItemHei / 2)+"' to='"+(tmLengendLeft + 10 + lenLineLen)+","+(tmLengendTop + tmLengFontHei + (j * tmLengItemHei) + tmLengItemHei / 2)+"' style='Z-INDEX:1;position:relative' strokeweight='" + (lineWid * 2) + "px' strokecolor='"+colors[j]+"'></v:line>"
       //改px result += "<v:rect filled='f'  stroked='f' style='Z-INDEX:1;top:"+ (tmLengendTop + tmLengFontHei + 20 + (j * tmLengItemHei)) +";left:" +(tmLengendLeft + lenLineLen + 20)+";width:"+ct[j].length * (tmLengFontWid + 5)+";height:" + tmLengItemHei + ";position:relative'>"
        result += "<v:rect filled='f'  stroked='f' style='Z-INDEX:1;top:"+ (tmLengendTop + tmLengFontHei + 20 + (j * tmLengItemHei)) +"px;left:" +(tmLengendLeft + lenLineLen + 20)+"px;width:"+ct[j].length * (tmLengFontWid + 5)+"px;height:" + tmLengItemHei + "px;position:relative'>"
       
       result += "<v:TextBox inset='0,0,0,0' style='font-size:"+10/zoomRate+"pt;TEXT-ALIGN:left'>"+ct[j]+"</v:TextBox></v:rect>"
    }
        
    return result;
}

//刷新图形
function ChartRefresh()
{
    getValue();
    drawChart(GchartType);
}